package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.lang.reflect.AccessibleObject;

public class EditPage extends Utils {
    public static String url = "http://8.156.68.126:8080/blog_edit.html";

    public EditPage() {
        super(url);
    }

    //不写标题
    public void EditPageFail_noTitle(){
        driver.findElement(By.cssSelector("#submit")).click();
        //等待弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        //切换
        Alert alert = driver.switchTo().alert();
        //点击确定
        alert.accept();
    }

    //不写内容
    public void EditPageFail_noContent() throws InterruptedException {
        //写标题
        driver.findElement(By.cssSelector("#title")).sendKeys("自动化测试演示标题");

        //清空内容
        WebElement ele = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span"));

        Actions action = new Actions(driver);

        //清除内容--全部删除
        Thread.sleep(2000);
        action.keyDown(Keys.ALT)
                .sendKeys(ele,Keys.ARROW_RIGHT)//鼠标移到右边
                .keyUp(Keys.ALT) //通过ctrl+shift+↑选中全部文本
                .keyDown(Keys.SHIFT)
                .sendKeys(Keys.ARROW_UP)
                .sendKeys(Keys.DELETE)//删除选中的本操作
                .perform();

        //防止还没有删除玩就提交,强等待
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("#submit")).click();
        //处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        //切换
        Alert alert = driver.switchTo().alert();
        //点击确定
        alert.accept();

        driver.quit();
    }

    public void EditPageSuc(){
        //写标题
        driver.findElement(By.cssSelector("#title")).sendKeys("自动化测试演示标题");
        //本身的内容
        driver.findElement(By.cssSelector("#submit")).click();

        //回到列表页
        //获取所有的文章，根据最后一篇文章来校验标题是否为我们刚创建的文章

        driver.quit();
    }
}
